<template>
  <el-form label-width="100px">
    <el-form-item label="sku名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input placeholder="价格(元)" v-model="skuParams.price"></el-input>
    </el-form-item>
    <el-form-item label="重量(克)">
      <el-input placeholder="重量(克)" v-model="skuParams.weight"></el-input>
    </el-form-item>
    <el-form-item label="sku描述">
      <el-input type="textarea" placeholder="sku描述" v-model="skuParams.skuDesc"></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true" label-width="80px">
        <el-form-item :label="item.attrName" v-for="item in attrArr" :key="item.id" style="padding-bottom:5px;">
          <el-select v-model="item.attrIdAndValueId" placeholder="请选择" style="width: 200px">
            <el-option :value="`${item.id}:${attrValue.id}`" v-for="(attrValue, index) in item.attrValueList"
              :key="attrValue.id" :label="attrValue.valueName">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true" label-width="80px">
        <el-form-item :label="item.saleAttrName" v-for="(item, index) in saleArr" :key="item.id"
          style="padding-bottom:5px;">
          <el-select v-model="item.saleIdAndValueId" placeholder="请选择" style="width: 200px">
            <el-option :value="`${item.id}:${saleAttrValue.id}`"
              v-for="(saleAttrValue, index) in item.spuSaleAttrValueList" :key="saleAttrValue.id"
              :label="saleAttrValue.saleAttrValueName">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column type="selection" width="100px" align="center">
        </el-table-column>
        <el-table-column label="图片" align="center">
          <template #="{ row, $index }">
            <img :src="row.imgUrl" alt="" style="width: 100px;">
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName">
        </el-table-column>
        <el-table-column label="操作">
          <template #="{ row, $index }">
            <el-button @click="handler(row)">设置默认</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { reqAttr } from '@/api/product/attr';
import { reqAddSku, reqImageList, reqSpuHasSaleAttr } from '@/api/product/spu';
import { skuData } from '@/api/product/spu/type';
import { ElMessage } from 'element-plus';

let $emit = defineEmits(['changeScene'])

//#region 数据定义
// 平台属性
let attrArr = ref<any>([])
// 销售属性
let saleArr = ref<any>([])
// 照片数据
let imgArr = ref<any>([])
// table组件实例
let table = ref<any>()
// SKU参数
let skuParams = reactive<skuData>({
  category3Id: "",
  spuId: "",
  tmId: "",
  skuName: "",
  price: "",
  weight: "",
  skuDesc: "",
  skuAttrValueList: [],
  skuSaleAttrValueList: [],
  skuDefaultImg: ''
})
//#endregion

// 取消按钮回调
const cancel = () => {
  $emit('changeScene', { flag: 0, params: 'update' })
}

const initSkuDate = async (c1Id: number | string, c2Id: number | string, spu: any) => {
  skuParams.category3Id = spu.category3Id
  skuParams.spuId = spu.id
  skuParams.tmId = spu.tmId
  let result: any = await reqAttr(c1Id, c2Id, spu.category3Id)
  let result1: any = await reqSpuHasSaleAttr(spu.id)
  let result2: any = await reqImageList(spu.id)

  attrArr.value = result.data
  saleArr.value = result1.data
  imgArr.value = result2.data
}

// 设置默认图片回调
const handler = (row: any) => {
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false)
  });
  table.value.toggleRowSelection(row, true)
  skuParams.skuDefaultImg = row.imgUrl
}

// 保存按钮回调
const save = async () => {
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(':');
      prev.push({
        attrId,
        valueId
      });
    }
    return prev
  }, [])
  skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':');
      prev.push({
        saleAttrId, saleAttrValueId
      });
    }
    return prev
  }, [])
  let result = await reqAddSku(skuParams)
  if (result.code == 200) {
    ElMessage({
      type: 'success',
      message: '添加SKU成功'
    })
    $emit('changeScene', { flag: 0, params: 'update' })
  } else {
    ElMessage({
      type: 'error',
      message: '添加SKU失败'
    })
  }
}

defineExpose({ initSkuDate })
</script>

<style scoped></style>